/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#include "sys_ext.h" 
#include "os_obj.h"
#include "mbapp.h"
#include "pincfg.h"
#include "com_def.h"

//RS2
#define mbPORT                       USART1
#define mbURT_IRQn                   USART1_IRQn
#define mbISR                        USART1_IRQHandler
#define RCU_USART                    RCC_APB2PERIPH_USART1
#define RCU_USARTRST                 RCC_APB2PERIPH_USART1
#define mbObj                        mb.obj01
#define mbCmdBuff                    mb.CmdBuff01

//RS485 Dir-Pin
#define mbRxEnable() RS485_DE=0
#define mbTxEnable() RS485_DE=1

#include "mbport_common.h"
#include "mb_fun.h"

/*****************************************************************************//*!
*
* @brief   port.
*             
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
static void mb_port_hal_cfg(void)
{
    intx_disable();
    
    RCC_APB2PeriphClockCmd(RCU_USART, ENABLE);
    RCC_APB2PeriphResetCmd(RCU_USARTRST, ENABLE);
    RCC_APB2PeriphResetCmd(RCU_USARTRST, DISABLE);
    
    intx_enable();
}

/*****************************************************************************//*!
*
* @brief   Timeout handle.
*          called from ext timer 1 ms
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void mb02_TimeoutHandle(void)
{
    mb_timer_handler();
}

/*****************************************************************************//*!
*
* @brief   app interface.
*        
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
static void mb_cb_set(void)
{
    mbObj.slave.reg_coils_cb = &eMBRegCoilsCB;
    mbObj.slave.reg_discrete_cb = &eMBRegDiscreteCB;
    mbObj.slave.reg_hold_cb = &eMBRegHoldingCB;
    mbObj.slave.reg_input_cb = &eMBRegInputCB; 
    
    mmb_cmd_buff_init(&mbObj, mbCmdBuff, 32, 16);
    
    mbObj.api = &mb_fun;
}


/*****************************************************************************//*!
* @brief   	com modle init .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void mb01_Init(mb_mode_type mbMode, uint32_t ulBaudRate, mb_parity_type eParity) 
{
    mb_port_Init(mbMode, ulBaudRate, eParity);
}
